<!DOCTYPE html>
<html lang="en">
  <body>
    <script type="text/javascript">
      function Animal(name, age) {
        this.name = name;
        this.age = age;
      }
      function objectFactory() {
        let object = null;
        let constructor = Array.prototype.shift.call(arguments);
        let result = null;
        // 判断参数是否是一个函数
        if (typeof constructor != "function") {
          console.error("类型错误");
          return;
        }
        //  新建一个空对象，对象的原型为构造函数的 proptype 对象
        object = Object.create(constructor.prototype);
        // 将 this 指向新建对象，并执行函数
        result = constructor.apply(object, arguments);
        // 判断返回对象
        let flag =
          result &&
          (typeof result === "object" || typeof result === "function");
        // 判断返回结果
        return flag ? result : object;
      }
      const animal = objectFactory(Animal, "Ketty", 20);
      console.log(animal);
    </script>
  </body>
</html>
